package myJs

import myJs.Implicits._
import myJs.Utils.{dataRow, dataValue}
import myJs.myPkg._
import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.myPkg.jquery._
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.{Element, document}
import scalatags.Text.all._

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/**
 * Created by yz on 6/5/2021
 */
@JSExportTopLevel("Download")
object Download {

  val formId = "form"
  val tableId = "table"
  val modalId = "modal"
  val annoTableId = "annoTable"
  val anaTableId = "anaTable"

  @JSExport("init")
  def init = {
    initTable
    registerEvent
    refreshTable
    refreshAnaTable
  }

  def anaTbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => {
    columnName match {
      case "Cohort" =>
        v
      case x =>
        val url = g.jsRoutes.controllers.DownloadController.download().url.toString
        a(href := s"${url}?fileName=${v}", v.toString)
    }
  }

  def refreshAnaTable = {
    val url = g.jsRoutes.controllers.ToolController.getFileData().url.toString
    Ajax.get(url = s"${url}?fileName=download.txt").map { xhr =>
      val rs = xhr.responseText.toJDJAn
      val columnNames = rs("columnNames").asInstanceOf[js.Array[String]]
      val columns = columnNames.map { columnName =>
        val fmt = anaTbFmt(columnName)
        ColumnOptions.field(columnName).title(columnName).sortable(false).formatter(fmt)
      }
      val options = TableOptions.data(rs("array")).columns(columns).search(false).pagination(false)
      $(s"#${anaTableId}").bootstrapTable(options)
    }
  }

  def expStyle(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[Any]) => columnName match {

    case _ => v
  }

  def tbFmt(columnName: String): js.Function = (v: js.Any, row: js.Dictionary[js.Any]) => {
    columnName match {
      case "file"=>
        val url=g.jsRoutes.controllers.DownloadController.download().url.toString
        a(href := s"${url}?fileName=${v}", v.toString)
      case _ => v
    }
  }

  def initTable = {
    initAnnoTable()
  }

  def initAnnoTable(tableJq: JQuery = $(s"#${annoTableId}")) = {
    val map = SeqMap("item" -> "Item", "file" -> "File")
    val columnNames = map.keyList
    val columns = columnNames.map { columnName =>
      val title = map.getOrElse(columnName, columnName)
      val fmt = tbFmt(columnName)
      val style = expStyle(columnName)
      val basicOptions = ColumnOptions.field(columnName).title(title).titleTooltip(title).sortable(true).formatter(fmt).cellStyle(style)
      columnName match {
        case x if List("size", "uploadTime", "fileName", "comment", "tags").contains(x) => basicOptions.
          sortable(false).operateable(true)
        case _ => basicOptions
      }
    }
    val options = TableOptions.columns(columns.toJSArray).pagination(false).search(false)
    tableJq.bootstrapTable(options)
  }

  def refreshTable = {
    case class FileData(item: String, file: String)
    val datas = js.Array(
      FileData("lncRNA and mRNA", "longRNAs_anno.txt"),
      FileData("circRNA", "circRNAs_anno.txt"),
      FileData("Pathway", "Pathways_anno.txt"),
      FileData("Tissue/Cell origin", "Origins_anno.txt"),
    )
    val data = datas.map { data =>
      js.Dictionary("item" -> s"${data.item}", "file" -> s"${data.file}")
    }
    $(s"#${annoTableId}").bootstrapTable("load", data)
  }


  def registerEvent = {

  }


}
